# Read in the CA DFW Oil Spill Incident Tracking data
ca_oil <- read_sf(here("ds394"), layer = "ds394") %>% 
  clean_names()

# Check the projection:
st_crs(ca_oil) # EPSG = 3310

# Read in the CA county data (TIGER shapefile):
ca_counties <- read_sf(here("ca_counties"), layer = "CA_Counties_TIGER2016") %>% 
  clean_names() %>% 
  select(name)

# Check the projection
st_crs(ca_counties) # EPSG = 3857

# Transform ca_counties into ca_oil
ca_counties <- st_transform(ca_counties, st_crs(ca_oil))

Interactive Map

# interactive mode
tmap_mode("view")

# tmap with CA and oil spill locations
tm_basemap("Esri.WorldStreetMap") +
tm_shape(ca_counties) +
  tm_fill(col = "navyblue", alpha = 0.75) +
  tm_borders(col = "black") +
tm_shape(ca_oil) +
  tm_dots(col = "gold",
          alpha = 0.8) 

Static Chloropleth

# Filter for inland only
ca_oil_static <- ca_oil %>% 
  filter(inlandmari == "Inland") 

# Join counties with oil dataset
chlor_data <- ca_counties %>%
  st_join(ca_oil_static) %>% 
  count(localecoun)

# Chloropleth Plot
ggplot(data = chlor_data) +
  geom_sf(aes(fill = n), color = "white", size = 0.1) +
  scale_fill_gradient(low = "gold", high = "navyblue") +
  theme_void() +
  labs(caption = "Chloropleth of counts of inland oil spills in 2008 in California",
       fill = "Number of oil spills\nwithin county") +
  theme(legend.title = element_text(face = "bold", size = 12, color = "black"),
        legend.text = element_text(face = "bold", size = 11, color = "black"),
        legend.position = "right",
        plot.caption = element_text(face = "bold", size = 11, color = "black"),
        plot.background = element_rect(fill = "white",
                                       color = "white"))